<?PHP
class Page{
  public $id      = -1;
  public $alias   = "";
  public $title   = "";
  public $ownerid = -1;
  public $owner   = false;
  public $menu    = -1;
  private $meta    = null;
  private $editor  = null;
  public $editorText = "";
  private $editorContent = "";
  private $loadForBreadcrumb = false;
  
  function loadProperties($alias,$loadforbreadcrumb = false){
    if ($alias == "") {
      $alias = "home";
    }
    $this->alias = $alias;
    $row = $GLOBALS['db']->ReadRow("SELECT * FROM {'dbprefix'}pages 
                           WHERE alias = '".$alias."'");
    $this->load($row,$loadforbreadcrumb);

  }
  
  function loadPropertiesById($id){
    $row = $GLOBALS['db']->ReadRow("SELECT * FROM {'dbprefix'}pages 
                           WHERE id = '".$id."'");
    $this->load($row);
  }
  
  function load($data,$loadforbreadcrumb = false){
    if($data){
      $this->id            = $data->id;
      $this->title         = $data->title;
      $this->ownerid       = $data->owner;
      $this->menu          = $data->menu;
      $this->alias         = $data->alias;
      $this->editorContent = $data->content;
      $this->loadforbeadrcumb = $loadforbreadcrumb;
      $this->editorText = $data->editor;
      if(!isset($_SESSION['user']) || !isset($_SESSION['user']->role) || !$_SESSION['user']->role->canAccess($this)){
        throw new AccessDeniedException("Access denied: ".$this->alias);
      }
    }
    else{
      throw new FileNotFoundException("File Not Found: ".$this->alias);
    }
  }

  public function getMeta(){
    if($this->meta == null){
      $this->meta          = new Meta();
      $this->meta->pageid  = $this->id;
      $this->meta->load();
    }
    return $this->meta;
  }

  public function getEditor(){
    if($this->editor == null){
      $this->editor = new $this->editorText($this);
    }
    return $this->editor;
  }

  public function setEditor(Editor $editor){
    $this->editor = $editor;
  }

  function getContent(){
    EventManager::raiseEvent("content_top",".",$this);
    $this->getEditor()->display();
    EventManager::raiseEvent("content_bottom",".",$this);
  }

  function getOwner($loadforbreadcrumb = false){
    if(!$this->owner){
      $alias = $GLOBALS['db']->ReadField("SELECT alias FROM {'dbprefix'}pages 
                                          WHERE id = '".$this->ownerid."'");
      if($alias){
        $this->owner = new Page();
        $this->owner->loadProperties($alias,$loadforbreadcrumb);
      }
    }
    return $this->owner;
  }

  function getBreadcrumb(){
    if(Cache::contains("breadcrumb",$this->id)){
      $res = Cache::getData("breadcrumb",$this->id);
    }
    else{
      if(!$this->owner) $this->getOwner(true);
      if($this->owner){
        $res = $this->owner->getBreadcrumb();
      }
      $res[] = array($this->alias,$this->title);
      Cache::setData("breadcrumb",$this->id,$res);
    }
    return $res;
  }

  function save(){
    $title   = $GLOBALS['db']->EscapeString($this->title);
    $alias   = $GLOBALS['db']->EscapeString($this->alias);
    $menu    = $GLOBALS['db']->EscapeString($this->menu);
    $id      = $GLOBALS['db']->EscapeString($this->id);
    $ownerid = $GLOBALS['db']->EscapeString($this->ownerid);
    $editorContent = $GLOBALS['db']->EscapeString($this->editorContent);
    $res = $GLOBALS['db']->Execute("UPDATE {'dbprefix'}pages SET
                                    title    = '".$title."',
                                    alias    = '".$alias."',
                                    menu     = '".$menu."' ,
                                    content  = '".$editorContent."',
                                    owner  = '".$ownerid."'  
                                    WHERE id = '".$id."'");
    $this->getMeta()->save();
    if($res){
      $args['title']   = $this->title;
      $args['alias']   = $this->alias;
      $args['menu']    = $this->menu;
      $args['id']      = $this->id;
      $args['ownerid'] = $this->ownerid;
      $args['content'] = $this->content;
      EventManager::raiseEvent("page_saved","../",$args);
    }
    Cache::clear("breadcrumb");
    Cache::clear("page");
    return $res;
  }

  function create($alias,$editor,$title){
    $title = $GLOBALS['db']->EscapeString($title);
    $res = $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}pages (alias,editor, title, content) VALUES ('".$alias."','".$editor."','".$title."','<h1>".$title."</h1>')");
    if($res){
      $res            = $GLOBALS['db']->InsertID();
      $args['id']     = $res;
      $args['editor'] = $editor;
      $args['alias']  = $alias;
      $admin = new Role();
      $admin->load(2);
      $admin->allowAccessByID($args['id']);
      EventManager::raiseEvent("page_created","../",$args);
    }
    Cache::clear("page");
    return $args['id'];
  }

  function delete(){
    $res = $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}pages WHERE alias = '".$this->alias."'");
    if($res){
      $args['alias'] = $this->alias;
      EventManager::raiseEvent("page_deleted","../",$args);
    }
    Cache::clear("breadcrumb");
    Cache::clear("page");
    return $res;
  }

  function getPagesTypes(){
    return $GLOBALS['db']->ReadRows("SELECT * FROM {'dbprefix'}pagetypes");
  }

  function displayBreadcrumb($separator,$class,$idpraefix){
    $this->getEditor()->displayBreadcrumb($separator,$class,$idpraefix);
  }

  static function getPagesByDir($dir){
    $dir = $GLOBALS['db']->EscapeString($dir);
    if(substr($dir,0,1) == "/"){
      $dir = substr($dir,1);
    }
    return $GLOBALS['db']->ReadRows("SELECT * FROM {'dbprefix'}pages 
                           WHERE alias LIKE '".$dir."%'");
    
  }

  public function getEditorContent(Editor $editor){
    return $this->editorContent;
  }

  public function setEditorContent($content){
    $this->editorContent = $content;
  }

  public function getDir(){
    $res = "";
    if(strpos($this->alias,"/") > -1){
      $parts = explode('/',$this->alias);
      for($i = 0;$i<sizeOf($parts)-1;$i++){
        $res .= $parts[$i]."/";
      }
    }
    return $res;
  }

  public function getLocalAlias(){
    $res = "";
    $parts = explode('/',$this->alias);
    $res = $parts[sizeOf($parts)-1];
    return $res;
  }
}

?>
